/*
 *  Copyright 2011-2012 Redia A/S
 * 
 *  This file is part of Redias Android Utilities.
 *
 *  Redias Android Utilities is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redias Android Utilities is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redias Android Utilities.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.lib.processing;

import android.os.AsyncTask;

/**
 * 
 * @author boll
 *
 * Wrapper class for AsyncTask which allows more explicit 
 * thread pool management.
 * 
 * Use this class by overriding it and make sure that you call
 * super.onPostExecute() and super.onCancelled().
 * ManagedAsyncTask instances are "launched" by passing them to 
 * AsyncTaskManager.taskReady(). Launching will be delayed until 
 * such as time as a thread pool thread becomes available.
 *
 * @param <Params>
 * @param <Progress>
 * @param <Result>
 */
public abstract class ManagedAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {

	private Params[] params;
	
	protected void onPostExecute(Result result) {
		AsyncTaskManager.getInstance().taskFinished(this);
		super.onPostExecute(result);
    }
	
	protected void onCancelled() {
		AsyncTaskManager.getInstance().taskCancelled(this);
		super.onCancelled();
	}
	
	public void executeManaged(Params...params) {
		this.params = params;
		AsyncTaskManager.getInstance().taskReady(this);
	}
	
	public void execute() {
		execute(this.params);
	}
	
}
